<!DOCTYPE html>
<html>
<head lang="en">
    <title>您搜索的内容是:</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>

</head>
<script>
    $(function () {
        var params = getParams();
        //获取传递过来的关键字
        var keyword = decodeURIComponent(params.keyword);
        $(".search p").html("您搜索的内容是:" + keyword);
        //页面头显示
        $.get(baseUrl + "/searchs/count", {keyword: keyword}, function (data) {
            if (data.strategyCount != 0) {
                $("#strategy").trigger("click");
            }
            if (data.travelCount != 0) {
                $("#travel").trigger("click");
            }
            $("#pills-tab").renderValues(data, {
                getHref: getHref
            });
        });

        //全局变量如下：
        var type;
        var pages = 2;//总页数
        var Arr = [];//用户存储游记文章和攻略文章的容器
        var collect = false; //标记是已收藏还是没收藏，告知后台去删或新增
        var strategydetailId;//用来存放游记id


        $(".clickTab").click(function () {

            //页面体显示（第一页的）
            type = $(this).data("type");
            $.get(baseUrl + "/searchs", {keyword: keyword, currentPage: 1, type: type}, function (data) {
                Arr = [];
                console.log(data);
                if (type == 1) {
                    if (data.total != 0) {
                        $.merge(Arr, data.list);//合并数据,把第二个参数的数据合并到第一个参数的容器中
                        var json = {list: Arr};
                        $("#pills-strategy").renderValues(json,{
                            getHref: getHref
                            /*getCollect: function (item, value) {
                                $(item).attr("data-id", value);
                            }*/
                            /*getEcho: function (item,value) {
                                strategydetailId = value;
                                //进页面就发ajax去后台查，如果已经收藏，就显示取消收藏，如果没，就显示收藏
                                $.get(baseUrl + '/collects/code', {strategydetailId: strategydetailId}, function (data) {
                                    if (data.code) {
                                        $("#sctxt").text('取消收藏');
                                        collect = true;
                                    } else {
                                        $("#sctxt").text('收藏');
                                        collect = false;
                                    }
                                })
                            }//getEcho函数的后括号*/
                        });
                    }
                }
                if (type == 2) {
                    if (data.total != 0) {
                        $.merge(Arr, data.list);//合并数据,把第二个参数的数据合并到第一个参数的容器中
                        var json = {list: Arr};
                        $("#pills-travel").renderValues(json, {
                            getHref: getHref
                        });
                    }
                    pages = data.pages;
                }
                // strategydetailId = $(this).data('id');





            })//这是第一页ajax的后括号
        });//这里是点击按钮的后括号
        var currentPage = 2;//当前页


        //分页方法
        function query() {
            $.get(baseUrl + "/searchs", {keyword: keyword, currentPage: currentPage, type: type}, function (data) {
                if (type == 1) {
                    $.merge(Arr, data.list);//合并数据,把第二个参数的数据合并到第一个参数的容器中
                    var json = {list: Arr};//用于render-loop="list"
                    $("#pills-strategy").renderValues(json, {
                        getHref: getHref,
                        getCollect: function (item, value) {
                            $(item).attr("data-id", value);
                        }
                    });//使用合并后的容器来进行渲染,但是必须要有list的属性名称

                    currentPage++;//当前页数+1
                    pages = data.pages;//设置总页数
                    loading = false;//标记查询结束
                }

                if (type == 2) {

                    $.merge(Arr, data.list);//合并数据,把第二个参数的数据合并到第一个参数的容器中
                    var json = {list: Arr};//用于render-loop="list"
                    $("#pills-travel").renderValues(json, {
                        getHref: getHref
                    });//使用合并后的容器来进行渲染,但是必须要有list的属性名称
                    currentPage++;//当前页数+1
                    pages = data.pages;//设置总页数
                    loading = false;//标记查询结束
                }
            })
            currentPage++;
        }

        //监听滚动事件
        $(window).scroll(function () {
            if (($(document).scrollTop() + $(window).height()) >= $(document).height() - 1) {
                if (currentPage <= pages) {
                    console.log("分页方法调用了");
                    query();

                } else {
                    $(document).dialog({
                        type: 'notice',
                        infoText: '已经到最底部了!',
                        autoClose: 1000,
                        position: 'bottom'  // center: 居中; bottom: 底部
                    });
                }
            }
        });


        /*$('.btn').click(function () {
            alert("ddd")
        })*/
    });
</script>
<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <p style="color: white; font-size: 20px"></p>
            </div>
        </div>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item" style="width: 33%">
        <a role="button" id="strategy" id="strategy" class="nav-link clickTab active show" data-type="1"
           data-toggle="pill"
           href="#pills-strategy">
            攻略【<span class="strategy" render-html="strategyCount"></span>】
        </a>
    </li>
    <li class="nav-item" style="width: 33%">
        <a role="button" id="travel" id="travel" class="nav-link clickTab" data-type="2" data-toggle="pill"
           href="#pills-travel">
            游记【<span class="travel" render-html="travelCount"></span>】
        </a>
    </li>
</ul>
<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-strategy">

        <!--循环渲染-->
        <div render-loop="list">
            <div class="container commend">
                <a data-url="strategyDetails.html?id=" render-fun="getHref" render-key="list.id">
                    <p render-html="list.title"></p>
                    <img render-src="list.coverUrl">
                </a>
                <!--<button class="btn">11</button>-->
            </div>
            <hr>
        </div>
    </div>

    <div class="tab-pane fade" id="pills-travel">

        <!--循环渲染-->
        <div render-loop="list">
            <div class="container commend">
                <a data-url="travelContent.html?id=" render-fun="getHref" render-key="list.id">
                    <p render-html="list.title"></p>
                    <img render-src="list.coverUrl">
                </a>
            </div>
            <hr>
        </div>
    </div>
</div>
</body>
</html>